********************************************************************************
********************************************************************************
* to obtain the dataset
*	1) obtain total damage in each county
*	2) obtain average MD for each county
********************************************************************************
********************************************************************************
clear all
version 11
set more off

global root "~\APEEP_2008\results"
cd $root



**************************************
* obtain total damages by source
**************************************
use .\Stata_working\data_e_with_fips_all_appended, clear
merge 1:1 apeep_results_running using .\Stata_working\data_md_with_fips_all_appended
count if fips_md!=fips_e // should be zero

local pollutant nh3 pm25 nox so2 voc
foreach pol of local pollutant {
	gen d_`pol' = e_`pol'*md_`pol'
}


***********************************************
* obtain total damages and emissions by county
* by summing over sources for each county
***********************************************
gen fips = fips_md
collapse (sum) e_nh3- e_voc d_nh3- d_voc (mean) md_nh3- md_voc, by(fips)


***********************************************
* obtain average MD
***********************************************

local pollutant nh3 pm25 nox so2 voc
foreach pol of local pollutant {
	count if e_`pol'==0
	list if e_`pol'==0
}

foreach pol of local pollutant {
	gen md_county_avg_`pol' = d_`pol' / e_`pol' if e_`pol'!=0
	replace md_county_avg_`pol' = md_`pol' if e_`pol'==0
}

compress
saveold .\Stata_working\data_md_county_average, version(11) replace
outsheet using .\Stata_working\data_md_county_average.csv, comma nolabel replace
